package cn.xiaosiyuan.util.date.impl;

import cn.xiaosiyuan.util.date.IMonUtil;
import cn.xiaosiyuan.util.date.IDateUtil;

import java.time.LocalDate;
import java.time.Period;

public class DateUtil implements IDateUtil, IMonUtil {

    private LocalDate dateR;
    private LocalDate dateL;


    /*
    -
        <
        <=
    <
        _
        <
        <=
    <=
        _
        <
        <=
     */

    public boolean default_value = false;

    private LocalDate getDate(CharSequence date) {
        return LocalDate.parse(date);
    }

    public boolean isBetween(CharSequence date, String left, String right) {
        boolean isBetween = false;
        switch (left) {

            case EMPTY: {
                switch (right) {
                    /*
                    date
                     */
                    case EMPTY: {
                        isBetween = default_value;
                        break;
                    }
                    /*
                    date < dateR
                     */
                    case UN_EQUAL: {
                        Period period = Period.between(getDate(date), getDateR());
                        if (period.getYears() > 0 || period.getMonths() > 0 || period.getDays() > 0)
                            isBetween = true;
                        break;
                    }
                    /*
                    date <= dateR
                     */
                    case EQUAL: {
                        Period period = Period.between(getDate(date), getDateR());
                        if ((period.getYears() > 0 || period.getMonths() > 0 || period.getDays() > 0)
                                || (period.getYears() == 0 && period.getMonths() == 0 && period.getDays() == 0))
                            isBetween = true;
                        break;
                    }
                }
                break;
            }
            case UN_EQUAL: {
                switch (right) {
                    /*
                    dateL < date
                     */
                    case EMPTY: {
                        Period period = Period.between(getDateL(), getDate(date));
                        if (period.getYears() > 0 || period.getMonths() > 0 || period.getDays() > 0)
                            isBetween = true;
                        break;
                    }
                    /*
                    dateL < date < dateR
                     */
                    case UN_EQUAL: {
                        Period periodL = Period.between(getDateL(), getDate(date));
                        if (periodL.getYears() > 0 || periodL.getMonths() > 0 || periodL.getDays() > 0) {
                            Period periodR = Period.between(getDate(date), getDateR());
                            if (periodR.getYears() > 0 || periodR.getMonths() > 0 || periodR.getDays() > 0)
                                isBetween = true;
                        }

                        break;
                    }
                    /*
                    dateL < date <= dateR
                     */
                    case EQUAL: {
                        Period periodL = Period.between(getDateL(), getDate(date));
                        if (periodL.getYears() > 0 || periodL.getMonths() > 0 || periodL.getDays() > 0) {
                            Period periodR = Period.between(getDate(date), getDateR());
                            if ((periodR.getYears() > 0 || periodR.getMonths() > 0 || periodR.getDays() > 0)
                                    || (periodR.getYears() == 0 || periodR.getMonths() == 0 || periodR.getDays() == 0))
                                isBetween = true;
                        }

                        break;
                    }
                }
                break;
            }
            case EQUAL: {
                switch (right) {
                    /*
                    dateL <= date
                     */
                    case EMPTY: {
                        Period period = Period.between(getDateL(), getDate(date));
                        if ((period.getYears() > 0 || period.getMonths() > 0 || period.getDays() > 0)
                                || (period.getYears() == 0 || period.getMonths() == 0 || period.getDays() == 0))
                            isBetween = true;
                        break;
                    }
                    /*
                    dateL <= date < dateR
                     */
                    case UN_EQUAL: {
                        Period periodL = Period.between(getDateL(), getDate(date));
                        if ((periodL.getYears() > 0 || periodL.getMonths() > 0 || periodL.getDays() > 0)
                                || (periodL.getYears() == 0 || periodL.getMonths() == 0 || periodL.getDays() == 0)) {
                            Period periodR = Period.between(getDate(date), getDateR());
                            if (periodR.getYears() > 0 || periodR.getMonths() > 0 || periodR.getDays() > 0)
                                isBetween = true;
                        }

                        break;
                    }
                    /*
                    dateL <= date <= dateR
                     */
                    case EQUAL: {
                        Period periodL = Period.between(getDateL(), getDate(date));
                        if ((periodL.getYears() > 0 || periodL.getMonths() > 0 || periodL.getDays() > 0)
                                || (periodL.getYears() == 0 || periodL.getMonths() == 0 || periodL.getDays() == 0)) {
                            Period periodR = Period.between(getDate(date), getDateR());
                            if ((periodR.getYears() > 0 || periodR.getMonths() > 0 || periodR.getDays() > 0)
                                    || (periodR.getYears() == 0 || periodR.getMonths() == 0 || periodR.getDays() == 0))
                                isBetween = true;
                        }

                        break;
                    }
                }
                break;
            }
        }
        return isBetween;
    }


    public LocalDate getDateR() {
        return dateR;
    }

    public void setDateR(CharSequence dateR) {
        this.dateR = getDate(dateR);
    }


    public LocalDate getDateL() {
        return dateL;
    }

    public void setDateL(CharSequence dateL) {
        this.dateL = getDate(dateL);
    }



    @Override
    public boolean compare(CharSequence leftOperand, String operatorLeft, CharSequence middleOperand, String operatorRight, CharSequence rightOperand) {
        setDateL(leftOperand);
        setDateR(rightOperand);
        return isBetween(middleOperand, operatorLeft, operatorRight);
    }






}
